import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import spring_annotation.config.MainTxConfig;
import spring_annotation.service.StudentService;
import spring_annotation.service.UserService;

import java.util.concurrent.*;

public class IOCTest_Tx {

    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MainTxConfig.class);

    @Test
    public void test01(){
        UserService userService = ac.getBean(UserService.class);
        userService.insertUser();
    }

    @Test
    public void test02(){
        StudentService studentService = ac.getBean(StudentService.class);
        int corePoolSize = 2;
        int maximumPoolSize = 16;
        long keepAliveTime = 30;
        TimeUnit unit = TimeUnit.SECONDS;
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(300);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize, maximumPoolSize, keepAliveTime,
                unit, workQueue, new ThreadPoolExecutor.CallerRunsPolicy());
        executor.prestartAllCoreThreads(); // 预启动所有核心线程
        for (int i = 0; i < 700000; i++) {
            executor.execute(()->studentService.insertStudent());
        }
//        executor.shutdown();
    }

    @Test
    public void test03(){
        StudentService studentService = ac.getBean(StudentService.class);
        int corePoolSize = 2;
        int maximumPoolSize = 16;
        long keepAliveTime = 30;
        TimeUnit unit = TimeUnit.SECONDS;
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(30);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize, maximumPoolSize, keepAliveTime,
                unit, workQueue, new ThreadPoolExecutor.CallerRunsPolicy());
        executor.prestartAllCoreThreads(); // 预启动所有核心线程
        for (int i = 0; i < 1000; i++) {
//            studentService.insertClass();
            executor.execute(()->studentService.insertClass());
        }
//        executor.shutdown();
    }

    @Test
    public void test04(){
        StudentService studentService = ac.getBean(StudentService.class);
        int corePoolSize = 2;
        int maximumPoolSize = 16;
        long keepAliveTime = 30;
        TimeUnit unit = TimeUnit.SECONDS;
        LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(30);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize, maximumPoolSize, keepAliveTime,
                unit, workQueue, new ThreadPoolExecutor.CallerRunsPolicy());
        executor.prestartAllCoreThreads(); // 预启动所有核心线程
        for (int i = 1; i <= 700000; i++) {
//            studentService.insertClass();
            int index = i;
            executor.execute(()->studentService.updateStudent(index));
        }
//        executor.shutdown();
    }


}
